package com.fz.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fz.entity.Log;
import com.fz.mapper.LogMapper;
import com.fz.service.LogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Map;

/**
 * <p>
 * 存储用户所有操作日志 服务实现类
 * </p>
 *
 * @author hyr
 * @since 2025-06-18
 */
@Service
public class LogServiceImpl extends ServiceImpl<LogMapper, Log> implements LogService {
    //实现对应用户的日志分页及筛选排序功能
    @Override
    public IPage<Log> userpage(Long userId, Integer pageNo, Integer pageSize , Map<String, Object> filters, Map<String, String> sort, String orderNumber) {
        QueryWrapper<Log> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        //处理筛选条件
        filters.forEach((k, v) -> {
            if (v != null && v instanceof ArrayList) {
                ArrayList<?> values = (ArrayList<?>) v;
                if (!values.isEmpty()) {
                    queryWrapper.in(StrUtil.toUnderlineCase(k), values);
                }
            }
        });
        // 处理排序条件
        if (sort != null && sort.containsKey("prop") && sort.containsKey("orders")) {
            String prop = StrUtil.toUnderlineCase(sort.get("prop"));
            String order = sort.get("orders");
            if ("ascending".equals(order)) {
                queryWrapper.orderByAsc(prop);
            } else if ("descending".equals(order)) {
                queryWrapper.orderByDesc(prop);
            }
        }
        if(StrUtil.isNotBlank(orderNumber)){
            queryWrapper.eq("order_number",orderNumber);
        }
        return this.page(new Page<>(pageNo, pageSize), queryWrapper);
    }
}
